package com.neusoft.neuiotms.dcam.fixed.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.service.AssetStatsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "资产统计")
@RestController
@RequestMapping(value = "/api/w/asset/stats")
public class AssetStatsController {

    @Autowired
    private AssetStatsService assetStatsService;

    @ApiOperation("按日、月、半年、年等维度统计资产")
    @GetMapping("/{category}/{tmeDim}")
    public ResultListDTO<Map<String, Object>> commonStats(@PathVariable final String category,
            @PathVariable final String tmeDim, Searchable searchable) {
        final List<Map<String, Object>> list = this.assetStatsService.commonStats(category, tmeDim, searchable);
        final ResultListDTO<Map<String, Object>> result = ResultListDTO.success(list);
        return result;
    }

    @ApiOperation("导出按日、月、半年、年等维度统计资产")
    @GetMapping(value = "/{category}/{tmeDim}/export")
    public void download(@PathVariable final String category,
            @PathVariable final String tmeDim, final HttpServletResponse response, Searchable searchable) {

        final Workbook workbook = this.assetStatsService.exportCommonStats(category, tmeDim, searchable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode(this.getExcelTitleName(category) + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            AssetStatsController.log.error("", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AssetStatsController.log.error("", e);
            }
        }
    }

    @ApiOperation("导出按日、月、半年、年等维度统计资产PDF")
    @GetMapping(value = "/{category}/{tmeDim}/export/pdf")
    public void downloadPdf(@PathVariable final String category,
            @PathVariable final String tmeDim, final HttpServletResponse response, Searchable searchable) {

        OutputStream out = null;
        try {

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode(this.getExcelTitleName(category) + ".pdf", "UTF-8")));
            out = response.getOutputStream();
            this.assetStatsService.exportCommonStatsPdf(category, tmeDim, searchable, out);
            out.flush();
        } catch (final Exception e) {
            AssetStatsController.log.error("", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AssetStatsController.log.error("", e);
            }
        }
    }

    @ApiOperation("按使用人维度统计资产")
    @GetMapping("/personal")
    public ResultListDTO<Map<String, Object>> personalStats(Searchable searchable) {
        final List<Map<String, Object>> list = this.assetStatsService.personalStats(searchable);
        final ResultListDTO<Map<String, Object>> result = ResultListDTO.success(list);
        return result;
    }

    @ApiOperation("导出按使用人维度统计资产")
    @GetMapping(value = "/personal/export")
    public void downloadPersonalStats(final HttpServletResponse response, Searchable searchable) {

        final Workbook workbook = this.assetStatsService.exportPersonalStats(searchable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("省联社机关部门个人资产报表.xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            AssetStatsController.log.error("", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AssetStatsController.log.error("", e);
            }
        }
    }

    @ApiOperation("导出按使用人维度统计资产")
    @GetMapping(value = "/personal/export/pdf")
    public void downloadPersonalStatsPdf(final HttpServletResponse response, Searchable searchable) {

        OutputStream out = null;
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("省联社机关部门个人资产报表.pdf", "UTF-8")));
            out = response.getOutputStream();
            this.assetStatsService.exportPersonalStatsPdf(searchable, out);
            out.flush();
        } catch (final Exception e) {
            AssetStatsController.log.error("", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AssetStatsController.log.error("", e);
            }
        }
    }

    @ApiOperation("资产整体情况分析")
    @GetMapping("/summary")
    public ResultListDTO<Map<String, Object>> summaryStats() {
        final List<Map<String, Object>> list = this.assetStatsService.summaryStats();
        final ResultListDTO<Map<String, Object>> result = ResultListDTO.success(list);
        return result;
    }

    @ApiOperation("盘点统计")
    @GetMapping("/inventory")
    public ResultListDTO<Map<String, Object>> inventoryStats(Searchable searchable) {
        final List<Map<String, Object>> list = this.assetStatsService.inventoryStats(searchable);
        final ResultListDTO<Map<String, Object>> result = ResultListDTO.success(list);
        return result;
    }

    @ApiOperation("导出盘点统计")
    @GetMapping(value = "/inventory/export")
    public void downloadInventoryStats(final HttpServletResponse response, Searchable searchable) {

        final Workbook workbook = this.assetStatsService.exportInventoryStats(searchable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("省联社机关部门资产盘点报表.xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            AssetStatsController.log.error("", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AssetStatsController.log.error("", e);
            }
        }
    }

    @ApiOperation("导出按使用人维度统计资产")
    @GetMapping(value = "/inventory/export/pdf")
    public void downloadInventoryStatsPdf(final HttpServletResponse response, Searchable searchable) {

        OutputStream out = null;
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("省联社机关部门资产盘点报表.pdf", "UTF-8")));
            out = response.getOutputStream();
            this.assetStatsService.exportInventoryStatsPdf(searchable, out);
            out.flush();
        } catch (final Exception e) {
            AssetStatsController.log.error("", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                AssetStatsController.log.error("", e);
            }
        }
    }

    private String getExcelTitleName(String category) {
        if ("1".equals(category)) {
            return "省联社机关部门固定资产报表";
        } else if ("2".equals(category)) {
            return "省联社机关部门低值易耗品报表";
        } else if ("3".equals(category)) {
            return "省联社机关部门消耗品报表";
        }
        return "";
    }

}
